﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApp3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //定时器
            DispatcherTimer timer = new DispatcherTimer()
            {
                Interval = TimeSpan.FromSeconds(1),
            };
            timer.Tick += (o, e) =>
            {
                _processBar.Value = _mediaPlayer.Position.TotalMilliseconds;
            };
            timer.Start();
        }

        /// <summary>
        /// 是否播放
        /// </summary>
        private bool isPlay = false;

        /// <summary>
        /// 播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Image_Play(object sender, MouseButtonEventArgs e)
        {
            isPlay = !isPlay;
            string dir = Environment.CurrentDirectory;
            if (isPlay)
            {
                string imgPath = System.IO.Path.Combine(dir, @"Resources\Pause.png");
                _imgPlay.Source = new BitmapImage(new Uri(imgPath));
                if (_mediaPlayer.Source != null)
                {
                    _mediaPlayer.Play();
                    _border.Visibility = Visibility.Collapsed;
                }
            }
            else
            {
                string imgPath = System.IO.Path.Combine(dir, @"Resources\Play.png");
                _imgPlay.Source = new BitmapImage(new Uri(imgPath));
                _mediaPlayer.Pause();
                _border.Visibility = Visibility.Visible;
            }
        }

        //选择视频文件
        private void Image_SelectMedia(object sender, MouseButtonEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog()
            {
                Filter = "视频文件(.mp4)|*.mp4"
            };
            if (openFileDialog.ShowDialog() == true)
            {
                _mediaPlayer.Source = new Uri(openFileDialog.FileName);

                _mediaPlayer.MediaOpened-= _mediaPlayer_MediaOpended;
                //媒体加载时完成触发（设置进度总时长是多少）
                _mediaPlayer.MediaOpened += _mediaPlayer_MediaOpended;
            }
        }

        private void _mediaPlayer_MediaOpended(object sender,RoutedEventArgs e)
        {
            if (_mediaPlayer.NaturalDuration.HasTimeSpan)
            {
                //获取媒体总时长
                _processBar.Maximum = _mediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds;
            }
        }

        //快退
        private void Image_Backward(object sender, MouseButtonEventArgs e)
        {
            _mediaPlayer.Position -= TimeSpan.FromSeconds(10);
        }

        //快进
        private void Image_Forward(object sender, MouseButtonEventArgs e)
        {
            _mediaPlayer.Position += TimeSpan.FromSeconds(5);
        }
    }
}
